﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Text;
using EntLibEC.Biz.Online;
using EntLibEC.Cmn;
using EntLibEC.Biz;
using EntLibEC.Info.Online;

namespace EntLibEC.Shopping.Include
{
    public partial class Poll : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label lblPollForm = (Label)this.Parent.FindControl("lblPollForm");

            if (lblPollForm != null)
            {
                lblPollForm.Text = GetPollForm();
            }

            BuildPollContent();
        }

        protected string GetPollForm()
        {
            Hashtable ht = new Hashtable(1);
            ht.Add("Status", (int)AppEnum.BiStatus.Valid);

            DataSet ds = PollManager.GetInstance().GetPollDs(ht);
            if (!Util.HasMoreRow(ds))
                return "";

            StringBuilder sb = new StringBuilder();

            string temp;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                temp = @"
						<form name='frmPoll" + dr["sysno"].ToString() + @"' target='mywin'>
							<input type='hidden' name='PollSysno' value='" + dr["sysno"].ToString() + @"'>
							<input type='hidden' name='PollItemSysno'>
							<input type='hidden' name='PollType'>
						</form>";
                sb.Append(temp);
            }

            return sb.ToString();
        }

        private void BuildPollContent()
        {
            Hashtable ht = new Hashtable(1);
            ht.Add("Status", (int)AppEnum.BiStatus.Valid);

            DataSet ds = PollManager.GetInstance().GetPollDs(ht);
            if (!Util.HasMoreRow(ds))
                LiteralContent.Text = string.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                PollInfo oPoll = PollManager.GetInstance().Load(Util.TrimIntNull(dr["sysno"]));
                string begin = "<h5>";

                sb.Append(begin);
                sb.Append(oPoll.PollName).Append("</h5>");
                sb.Append("<ul>");
                foreach (PollItemInfo oPollItem in oPoll.itemList.Values)
                {
                    sb.Append("					<li>");
                    sb.Append("						<input type='radio' name='option" + oPollItem.PollSysNo + "' onclick=\"frmPoll" + oPollItem.PollSysNo + ".PollItemSysno.value='" + oPollItem.SysNo + "'\" >");
                    sb.Append(                      oPollItem.ItemName);
                    sb.Append("					</li>");
                }

                sb.Append("</ul>");
                sb.Append("						<div id=\"poll-button\">");
                sb.Append("									<input type='button' value='查 看'class='cssButton' onclick=\"checkOption" + oPoll.SysNo + "('0')\">");
                sb.Append("									<input type='button' value='投 票'class='cssButton' onclick=\"checkOption" + oPoll.SysNo + "('1')\">");
                sb.Append("						</div>");


                string frmName = "frmPoll" + oPoll.SysNo;
                string javascript0 = @"
				<script language=javascript>
				function checkOption" + oPoll.SysNo + @"(polltype){
				document." + frmName + @".PollType.value = polltype;
				document." + frmName + @".action = '../CustomerService/Poll.aspx';
				document." + frmName + @".method = 'get';
				window.open('','mywin','height=450,width=660,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes');
				document." + frmName + @".submit();
				}
				</script>";
                sb.Append(javascript0);
                //sb.Append("</div>");

            }

            LiteralContent.Text = sb.ToString();
        }
    }
}